package com.exrobot.module.rbt.controller.admin.robottask;

import org.springframework.web.bind.annotation.*;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import jakarta.validation.constraints.*;
import jakarta.validation.*;
import jakarta.servlet.http.*;
import java.util.*;
import java.io.IOException;

import com.exrobot.framework.common.pojo.PageParam;
import com.exrobot.framework.common.pojo.PageResult;
import com.exrobot.framework.common.pojo.CommonResult;
import com.exrobot.framework.common.util.object.BeanUtils;
import static com.exrobot.framework.common.pojo.CommonResult.success;

import com.exrobot.framework.excel.core.util.ExcelUtils;

import com.exrobot.framework.apilog.core.annotation.ApiAccessLog;
import static com.exrobot.framework.apilog.core.enums.OperateTypeEnum.*;

import com.exrobot.module.rbt.controller.admin.robottask.vo.*;
import com.exrobot.module.rbt.dal.dataobject.robottask.RobotTaskDO;
import com.exrobot.module.rbt.service.robottask.RobotTaskService;

@Tag(name = "管理后台 - 机器人任务")
@RestController
@RequestMapping("/rbt/robot-task")
@Validated
public class RobotTaskController {

    @Resource
    private RobotTaskService robotTaskService;

    @PostMapping("/create")
    @Operation(summary = "创建机器人任务")
    @PreAuthorize("@ss.hasPermission('rbt:robot-task:create')")
    public CommonResult<RobotTaskDO> createRobotTask(@Valid @RequestBody RobotTaskSaveReqVO createReqVO) {
        return success(robotTaskService.createRobotTask(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新机器人任务")
    @PreAuthorize("@ss.hasPermission('rbt:robot-task:update')")
    public CommonResult<Boolean> updateRobotTask(@Valid @RequestBody RobotTaskSaveReqVO updateReqVO) {
        robotTaskService.updateRobotTask(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除机器人任务")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('rbt:robot-task:delete')")
    public CommonResult<Boolean> deleteRobotTask(@RequestParam("id") Long id) {
        robotTaskService.deleteRobotTask(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除机器人任务")
                @PreAuthorize("@ss.hasPermission('rbt:robot-task:delete')")
    public CommonResult<Boolean> deleteRobotTaskList(@RequestParam("ids") List<Long> ids) {
        robotTaskService.deleteRobotTaskListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得机器人任务")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('rbt:robot-task:query')")
    public CommonResult<RobotTaskRespVO> getRobotTask(@RequestParam("id") Long id) {
        RobotTaskDO robotTask = robotTaskService.getRobotTask(id);
        return success(BeanUtils.toBean(robotTask, RobotTaskRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得机器人任务分页")
    @PreAuthorize("@ss.hasPermission('rbt:robot-task:query')")
    public CommonResult<PageResult<RobotTaskRespVO>> getRobotTaskPage(@Valid RobotTaskPageReqVO pageReqVO) {
        PageResult<RobotTaskDO> pageResult = robotTaskService.getRobotTaskPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, RobotTaskRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出机器人任务 Excel")
    @PreAuthorize("@ss.hasPermission('rbt:robot-task:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportRobotTaskExcel(@Valid RobotTaskPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<RobotTaskDO> list = robotTaskService.getRobotTaskPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "机器人任务.xls", "数据", RobotTaskRespVO.class,
                        BeanUtils.toBean(list, RobotTaskRespVO.class));
    }

}